<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');
require_once('../../_inc/class.rglDocGen.php');

$sqlSel = "SELECT a.*  FROM rgl_rating r "
		." INNER JOIN rgl_azienda a ON a.id_azienda = r.id_azienda " 
		." WHERE r.id_rating = ".reqVarAS('id');
$objDB->query($sqlSel,'selRec');
if (!$acAnag = $objDB->fetch('selRec')) {
	die("rquePdf az - var. id '".reqVar('id')."' mancante o errato");
}

//lettura risposte memorizzate
$aQueRisp = array();
$sSql = "SELECT rq.str_que, data_ora  FROM rgl_ratque rq " 
		." WHERE rq.id_rating = ".reqVar('id');
$objDB->query($sSql,'selRec');
$acRQ = $objDB->fetch('selRec');
if ($acRQ && !empty($acRQ['str_que'])) {
	$aQueRisp = unsgzu($acRQ['str_que']);
}

$oDoc = new rglDocGen();
$oDoc->maxCol = 40;
$oDoc->pdfIntestaz = $acAnag['rag_soc']." - Questionario compilato il ".YMD2DMY(substr($acRQ['data_ora'],0,10));
$oDoc->bottomLeftText = date("d/m/Y H:i:s")." (".$_SERVER['HTTP_HOST'].RGL_URL_ROOT.")";

$oDoc->primaPag = true;
$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'AZIENDA');
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Ragione sociale');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>22,'alg'=>'L','txt'=>$acAnag['rag_soc']);
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Cod.fiscale');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'L','txt'=>$acAnag['cod_fisc']);
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Residenza');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>22,'alg'=>'L','txt'=>$acAnag['sede']);
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Cod.CCIAA');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'L','txt'=>$acAnag['cod_cciaa']);
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'QUESTIONARIO');
$oDoc->setCol($aCol);

$nS=0;
foreach($aQueRisp as $codSez => $aSez) {
	$nS++;
	$nD=0;
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'lab1','col'=>40,'alg'=>'L','txt'=>$nS.". ".$aSez['d']);
	$oDoc->setCol($aCol);
	foreach($aSez['a'] as $codDom => $aDom) {
		$nD++;
		$oDoc->nuovaRiga();
		$aCol = array('cla'=>'txt1','col'=>2,'alg'=>'R','txt'=>$nD.". ");
		$oDoc->setCol($aCol);
		$aCol = array('cla'=>'txt1','col'=>32,'alg'=>'L','txt'=>$aDom['d']);
		$oDoc->setCol($aCol);
		$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'L','txt'=>$aDom['r']);
		$oDoc->setCol($aCol);
	}
}

$sPdf = $oDoc->genPdf(array('orientaPagina'=>'P'));
headerDownLoad(strlen($sPdf),"quetionario_".$acAnag['cod_fisc']."_".substr($acRQ['data_ora'],0,10).".pdf");
echo $sPdf;
exit();
